package top.defaults.colorpicker;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * ColorPickerView
 *
 * @author: jjy
 * @since 2021-04-16
 */
public class ColorPickerView extends DirectionalLayout implements SliderChangeListener,
    ColorChangeListener, Component.EstimateSizeListener {

    private ColorWheelView colorWheelView;
    private BrightnessSliderView brightnessSliderView;
    private AlphaSliderView alphaSliderView;
    private int initialColor = Color.BLACK.getValue();

    private boolean isDialog;

    private int currentColor;

    private ColorChangeListener mColorChangeListener;

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     */
    public ColorPickerView(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     * @param defStyleAttr 样式值
     */
    public ColorPickerView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setEstimateSizeListener(this::onEstimateSize);
        isDialog = attrs.getAttr("isDialog").get().getBoolValue();
        setOrientation(VERTICAL);
        initView(this.getContext());

    }

    public void setColorChangeListener(ColorChangeListener colorChangeListener) {
        this.mColorChangeListener = colorChangeListener;
    }

    private void initView(Context context) {
        colorWheelView = new ColorWheelView(context, isDialog);
        colorWheelView.setColorChangeListener(this);
        float density = Utils.getScreenPiex(context).scalDensity;
        int margin = (int) (8 * density);
        LayoutConfig params = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
        addComponent(colorWheelView, params);
        setPadding(margin, margin, margin, margin);
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        return false;
    }

    /**
     * 设置着色器初始颜色
     *
     * @param color 颜色值
     * @param alphaView 设置亮度view
     * @param brightnessView 设置透明度view
     */
    public void setInitialColor(int color, AlphaSliderView alphaView, BrightnessSliderView brightnessView) {
        initialColor = color;
        colorWheelView.setColor(color, true);
        this.alphaSliderView = alphaView;
        this.brightnessSliderView = brightnessView;
        this.alphaSliderView.setBaseColor(colorWheelView.getCurrentColor(), true, true);
        this.brightnessSliderView.setBaseColor(colorWheelView.getCurrentColor(), true, true);
        this.alphaSliderView.setSliderChangeListener(this);
        this.brightnessSliderView.setSliderChangeListener(this);
        currentColor = initialColor;
        onColorChange(currentColor);
        mColorChangeListener.onColorChange(currentColor);
    }

    /**
     * 设置是否高亮
     *
     * @param enable 是否高亮
     */
    public void setEnabledBrightness(boolean enable) {
        if (alphaSliderView != null) {
            setEnabledAlpha(true);
        }
    }

    /**
     * 是否能设置透明度
     *
     * @param enable 是否能设置透明度
     */
    public void setEnabledAlpha(boolean enable) {
    }

    /**
     * 重新设置颜色
     */
    public void reset() {
        colorWheelView.setColor(initialColor, true);
    }

    @Override
    public void onColorChange(int color) {
        currentColor = color;
        alphaSliderView.setBaseColor(color, true, true);
        brightnessSliderView.setBaseColor(color, true, true);
        sliderPositionChange();
        this.invalidate();
    }

    @Override
    public void sliderPositionChange() {

        float[] hsv = colorWheelView.getCurrentHSV();
        int[] rgbColor = ColorConverUtils.hsvToRgb((float) hsv[0], (float) hsv[1],
            brightnessSliderView.getCurrentValue());

        alphaSliderView.setBaseColor(Color.rgb(rgbColor[0], rgbColor[1], rgbColor[2]),
            true, true);

        int alpha = (int) (alphaSliderView.getCurrentValue() * 255);

        int red = (int) rgbColor[0];
        int green = (int) rgbColor[1];
        int blue = (int) rgbColor[2];

        int newColor = Color.argb(alpha, red, green, blue);
        currentColor = newColor;

        mColorChangeListener.onColorChange(currentColor);

    }

    /**
     * 获取当前颜色
     *
     * @return 当前颜色
     */
    public int getCurrentColor() {
        if (currentColor == 0) {
            currentColor = Color.argb((int) (alphaSliderView.getCurrentValue() * 255), 255, 0, 0);
        }
        return currentColor;
    }
}
